#include<iostream>
#include<list>
using namespace std;
int main()
{
	int n = 5, m = 3;
	int tmp = m;
	list<int> l;
	for (int i = 0; i < n; i++)
	{
		l.push_back(i);
	}
	list<int>::iterator it = l.begin();
	while (l.size() > 1)
	{
		tmp = m;
		while (--tmp)
		{
			it++;
			if (it == l.end())
			{
				it = l.begin();
			}
		}
		it = l.erase(it);
		if (it == l.end())
		{
			it = l.begin();
		}
	}
	cout << *it << endl;
	}